package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.sql.Timestamp;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;

public class PaymentController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);

        HttpSession sessie = request.getSession();

        User user = (User) sessie.getAttribute("gebruikerinfo");
        Date datum = new Date();
        java.sql.Date sqldatum = new java.sql.Date(datum.getTime());

        if (request.getParameter("id") != null) {
            int courseid = Integer.parseInt(request.getParameter("id"));
            queryManager.addsubscribed(user.getID(), courseid, sqldatum);
            
        } else {
            if(request.getParameter("course1") != null && !request.getParameter("course1").isEmpty()){
            queryManager.addsubscribed(user.getID(), Integer.parseInt(request.getParameter("course1")), sqldatum);
            }
            
            if(request.getParameter("course2") != null && !request.getParameter("course2").isEmpty()){
            queryManager.addsubscribed(user.getID(), Integer.parseInt(request.getParameter("course2")), sqldatum);
            }
            
            if(request.getParameter("course3") != null && !request.getParameter("course3").isEmpty()){
            queryManager.addsubscribed(user.getID(), Integer.parseInt(request.getParameter("course3")), sqldatum);
            }
            
            if(request.getParameter("course4") != null && !request.getParameter("course4").isEmpty()){
            queryManager.addsubscribed(user.getID(), Integer.parseInt(request.getParameter("course4")), sqldatum);
            }
        }


        
        dbManager.closeConnection();

        RequestDispatcher dispatcher = request.getRequestDispatcher("/payment.jsp");
        dispatcher.forward(request, response);
    }
}
